<?php
namespace app\admin\model;
use think\Model;
use think\Db;
use think\facade\Config;

/**
 *
 * @package app\admin\model
 */
class BusUserAndChannel extends Model
{

    public function getListAll($request)
    {
        $data = $this->alias('uac')
        ->field('uac.*,au.name,au.username,concat( bc.channel_id , "/", bc.company_name ) as company_name,bc.user_name,bc.channel_phone,bc.status,bc.statusdesc,bc.oevrplus,bc.sms_balance,bc.level,k.cid_key')
        ->join('bus_channel bc','uac.channel_id=bc.channel_id')
        ->join('bus_channel_key k','uac.channel_id=k.cid')
        ->join('adm_user au','uac.user_id=au.id');

        switch (session('user_type')) {
            case 'admin':
                # code...
                break;
            case 'clerk':
                $data = $data->where('uac.user_id',session('user_id'));
                break;

            default:
                exit;
                break;
        }

        if($request['cid'])
        {
            $data = $data->where('uac.channel_id',$request['cid']);
        }
        $data = $data
        // ->where('uac.user_id',session("user_id"))
        ->order('id desc')->paginate($request['limit'])->toArray();

        // $ZD_REDIS_CONFIG = Config::get('ZD_REDIS_CONFIG');
        //连接本地的 Redis 服务
        // $redis = new \Redis();
        // $redis->connect($ZD_REDIS_CONFIG['IP'], $ZD_REDIS_CONFIG['PORT']);
        // $redis->auth($ZD_REDIS_CONFIG['PASSWORD']);//密码
        // $redis->select(4);



        foreach ($data['data'] as $key => $value) {
            switch ($value['status']) {
                case '0':
                    $data['data'][$key]['status'] = '冻结';
                    $data['data'][$key]['statusdesc'] = '冻结';
                    break;

                default:
                    $data['data'][$key]['status'] = '正常';
                    $data['data'][$key]['statusdesc'] = '正常';
                    break;
            }

            // $oevrplus    = $redis->get('P_'.$value['channel_id']);
            // $sms_balance = $redis->get('T_'.$value['channel_id']);

            // $data['data'][$key]['oevrplus']    = $oevrplus ? $oevrplus : '';
            // $data['data'][$key]['sms_balance'] = $sms_balance ? $sms_balance : '';

            $data['data'][$key]['name'] = $value['name'] . '(' . $value['username'];

            if(in_array($value['level'], [1,4]))
            {
                $data['data'][$key]['oevrplus'] = '<span style="color:#B9A523">'.$value['oevrplus'].'元</span>';
            }else{
                $data['data'][$key]['oevrplus'] = '<span style="color:green">'.$value['sms_balance'].'条</span>';
            }

            $data['data'][$key]['cid_key'] = '<a href="javascript:;" onclick="openkey(\'' . $value['cid_key'] . '\')">查看</a>';
        }

        return $data;
    }
}